package net.net23.sysdent.server.jdo.pojos;

import java.util.Date;
import java.util.Set;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import net.net23.sysdent.server.jdo.control.search.model.DbEntity;
import net.net23.sysdent.server.jdo.control.search.model.SearchJanitor;

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
public class Link extends DbEntity {

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long id;

	@Persistent
	private String name;

	@Persistent
	private String link;

	@Persistent
	private Long user;

	@Persistent
	private Date creationDate;

	@Persistent
	private Set<String> keyWords;

	public Link() {
		super();
	}

	public Link(String link, Long user) {
		this(link, link, user);
	}

	public Link(String name, String link, Long user) {
		super();
		setName(name);
		setLink(link);
		setUser(user);
		setCreationDate(new Date());
	}

	public Long getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getLink() {
		return this.link;
	}

	public void setLink(String link) {
		this.link = link;
	}

	public Long getUser() {
		return user;
	}

	public void setUser(Long user) {
		this.user = user;
	}

	public void setCreationDate(Date creationDate) {
		this.creationDate = creationDate;
	}

	public Date getCreationDate() {
		return creationDate;
	}

	public void setKeyWords(Set<String> keyWords) {
		this.keyWords = keyWords;
	}

	public Set<String> getKeyWords() {
		return keyWords;
	}

	public void rebuildIndexes() {
		setKeyWords(SearchJanitor.updateKeyWordsForSearchableEntity(this));
	}

	@Override
	public String getSearchField() {
		return getLink();
	}
}
